//
//  HJChatSessionModel.m
//  KaiChat
//
//  Created by lx on 2023/10/21.
//

#import "HJChatSessionModel.h"
#import "HJChatMessageModel.h"

@implementation HJChatSessionModel

MJLogAllIvars

+(NSArray *)mj_ignoredPropertyNames {
    return [self bg_ignoreKeys];
}

+ (NSArray *)bg_uniqueKeys {
    return @[@"sessionId"];
}

+ (NSArray *)bg_ignoreKeys {
    return @[@"thumbnailImage", @"avatarUrls", @"sessionNameAttributedstring", @"disturb", @"muteMessageUnReadCount"];
}

- (NSString *)bg_tableName {
    return [NSString stringWithFormat:@"%@_%@", NSStringFromClass(self.class), HJUserInfo.userId];
}

+ (NSString *)bg_tableName {
    return [NSString stringWithFormat:@"%@_%@", NSStringFromClass(self.class), HJUserInfo.userId];
}

+ (HJChatSessionModel *)sessionModelWithSessionId:(NSString *)sessionId sessionUserd:(nullable NSString *)sessionUserd chatType:(HJChatType)chatType sessionName:(nullable NSString *)sessionName {
    HJChatSessionModel *sessionModel = [[HJChatSessionModel alloc] init];
    sessionModel.sessionId = sessionId;
    sessionModel.chatType = chatType;

    if (chatType == HJChatTypeGroup) {
        sessionModel.groupId = sessionId;
    } else {
        sessionModel.sessionUserd = sessionUserd;
        sessionModel.receiveId = sessionUserd;
        if (TJIM_isSelf(sessionUserd)) {
            sessionModel.sessionId = sessionUserd;
        }
    }
    sessionModel.sessionName = sessionName;
    return sessionModel;
}

+ (HJChatSessionModel *)sessionModelWithMessageModel:(HJChatMessageModel *)messageModel {
    HJChatSessionModel *sessionModel = [[HJChatSessionModel alloc] init];
    sessionModel.content = messageModel.content;

    if (messageModel.chatType == HJChatTypeSingle) {
        
        sessionModel.sessionId = toString(messageModel.sessionId);
        sessionModel.sessionUid = messageModel.sessionUid;
        sessionModel.sessionUserd = messageModel.sessionUserd;

        if (TJIM_isSelf(messageModel.senderId)) {//自己发的
  
            sessionModel.sessionName = messageModel.receiverNickname;
            sessionModel.sessionAvatar = messageModel.receiverAvatar;

            sessionModel.senderId = HJUserInfo.userId;
            sessionModel.senderName = HJUserInfo.nickname;

            sessionModel.uid = messageModel.receiverId;
            sessionModel.receiveId = messageModel.receiverId;
            sessionModel.receiveName = messageModel.receiverNickname;
        } else { //如果是对方发过来的
            sessionModel.uid = messageModel.senderId;
            sessionModel.sessionName = messageModel.senderNickname;
            sessionModel.sessionAvatar = messageModel.senderAvatar;
            sessionModel.senderId = messageModel.senderId;
            sessionModel.receiveId = messageModel.receiverId;
            sessionModel.receiveName = messageModel.receiverNickname;
        }
    } else if (messageModel.chatType == HJChatTypeOfficial) {
        
        sessionModel.sessionName =  messageModel.sessionName;
        sessionModel.sessionAvatar =  messageModel.sessionAvatar;
        sessionModel.sessionId = messageModel.sessionId;
        sessionModel.senderId = messageModel.senderId;
        sessionModel.receiveId = messageModel.receiverId;
    } else {
        sessionModel.sessionName =  messageModel.sessionName;
        sessionModel.sessionId = messageModel.sessionId;
        sessionModel.sessionAvatar =  messageModel.sessionAvatar;
        sessionModel.groupId = toString(messageModel.groupId);
        sessionModel.senderId = messageModel.senderId;
        sessionModel.senderName = messageModel.senderNickname;
    }
    
    sessionModel.unReadCount = messageModel.unReadCount;
    sessionModel.systemMsgType = messageModel.systemMsgType;
    sessionModel.chatType = messageModel.chatType;
    sessionModel.officialType = messageModel.officialType;
    sessionModel.messageType = messageModel.messageType;
    sessionModel.timestamp = messageModel.timestamp;
    return sessionModel;
}

+ (HJChatSessionModel *)modelWithBlock:(void (^)(HJChatSessionModel *model))block {
    HJChatSessionModel *model = [[HJChatSessionModel alloc] init];
    if (block) block(model);
    return model;
}
 
- (NSString *)getMessgetContent {
//    if (_messageType == HJMessageTypeText || _messageType == HJMessageTypeSystem || _messageType == HJMessageTypeOtherSystem) return self.content;
    if (_messageType == HJMessageTypeImage) return @"[Photo]";
    if (_messageType == HJMessageTypeVideo) return @"[Video]";
    if (_messageType == HJMessageTypeVoice) return @"[Audio]";
    if (_messageType == HJMessageTypeTransfer) return @"[Transfer]";
    if (_messageType == HJMessageTypeOther) return @"[File]";
    return self.content;
    
}


@end

 
